package com.sgq.vue.controller.picture;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgq.vue.entity.picture.Tag;
import com.sgq.vue.result.Result;
import com.sgq.vue.service.picture.TagService;
import com.sgq.vue.vo.query.picture.TagQueryVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author: 83778
 * @date: 2024-01-05 20:00
 * @desc:
 */
@Api(tags = "标签分区")
@RestController
@RequestMapping("/api/picture/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @PostMapping("/add")
    public Result add(@RequestBody Tag tag){

        boolean save = tagService.save(tag);
        return Result.ok(save);
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody Tag tag){
        System.out.println("tag = " + tag);
        String color = tag.getColor();
        color = color == null ? "" : color;
        tag.setColor(color);
        boolean update = tagService.updateById(tag);
        return Result.ok(update);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        boolean hasBeenUsed = tagService.check(id);
        if (hasBeenUsed){
            return Result.error().message("该tag被图片使用,无法删除");
        }else {
            boolean b = tagService.removeById(id);
            return Result.ok(b);
        }
    }

    @GetMapping("/findTagByPage")
    public Result findTagByPage(TagQueryVo tagQueryVo){
        IPage page = tagService.findTagByPage(tagQueryVo);
        return Result.ok(page);
    }

    @GetMapping("/list/{userId}")
    public Result list(@PathVariable Long userId){
        List<Tag> list = tagService.findTagByCreatorId(userId);
        return Result.ok(list);
    }

    @GetMapping("/findAllTag")
    public Result findAllTag(){
        return Result.ok(tagService.list());
    }

}
